package com.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.dao.LoginDao;
import com.po.UserPO;

@Controller
public class LoginAction  {
	@Autowired
	private LoginDao loginDao;
	private String username;
	private String password;
	
	
	/**
	 * 跳转到登录页面
	 */
	public String preLogin(){
		return "login";
	}
	
	/**
	 * 验证登录信息
	 * @return
	 */
	public String login() {
		Map<String, String> map = new HashMap<String, String>();
		HttpServletRequest request = ServletActionContext.getRequest();
		
		map.put("username", username);
		map.put("password", password);
		
		HttpServletResponse response = ServletActionContext.getResponse();
		UserPO user = loginDao.findUserInfo(map); 
		try {
			response.setContentType("text/html;charset=utf-8");
			
			if (user != null) {
				HttpSession session = request.getSession();
				session.setAttribute("Hello", "You");			
				response.getWriter().write("EC001");
			} else {
				response.getWriter().write("EC002");
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 验证session跳转到home界面
	 * @return
	 */
	public String show(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		if (session.getAttribute("Hello") != null) {
			return "show_home";
		} else {
			return null;
		}
	}
	
	public String showIframe(){
		return "show_iframe";
	}
	
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}
